package Map_Traversal;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:38917
 * Date:2025/4/17
 * Time:13:11
 */
public class Demo {
    //keySet
    public static void main(String[] args) {
        Map<Integer,String> map = new HashMap<>();
        map.put(1,"a");
        map.put(2,"b");
        map.put(3,"c");
        Set<Integer> integers = map.keySet();
        //foreach
        for (Integer integer : integers) {
            System.out.println(integer + " " + map.get(integer));
        }
        integers.remove(1);
        System.out.println("====================================================");
        //iterator
        Iterator<Integer> iterator = map.keySet().iterator();
        while (iterator.hasNext()) {
            int key = iterator.next();
            if (key == 1) {
            iterator.remove();
            }
            System.out.println(key + " " + map.get(key));
        }
    }
    //entrySet
    /*public static void main(String[] args) {
        Map<Integer,String> map = new HashMap<>();
        map.put(1,"a");
        map.put(2,"b");
        map.put(3,"c");
        Set<Map.Entry<Integer, String>> entries = map.entrySet();
        //foreach
        for (Map.Entry<Integer, String> entry : entries) {
            System.out.println(entry.getKey() + " " + entry.getValue());
        }
        System.out.println("====================================================");
        //iterator
        Iterator<Map.Entry<Integer, String>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }*/
    //values
    /*public static void main(String[] args) {
        Map<Integer,String> map = new HashMap<>();
        map.put(1,"a");
        map.put(2,"b");
        map.put(3,"c");
        Collection<String> values = map.values();
        //foreach
        for (String value : values) {
            System.out.println(value);
        }
        System.out.println("====================================================");
        //iterator
        Iterator<String> iterator = map.values().iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }

    }*/
    //使用keySet()是迭代器的错误用法
    /*public static void main(String[] args) {
        Map<Integer,String> map = new HashMap<>();
        map.put(1,"a");
        map.put(2,"b");
        map.put(3,"c");
        map.put(4,"d");
        Iterator<Integer> iterator = map.keySet().iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next() + " " + map.get(iterator.next()));
        }
    }*/
}
